package mysqlpkg

import (
	"gitee.com/wanjimao/dongli-kit/pkg/setting"
	//"dance-speedy-core/pkg/curlservice"
	"go.uber.org/fx"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"os"
)

type Param struct {
	fx.In
	MysqlDanceDb *gorm.DB `name:"MysqlDanceDb"`
}

type Result struct {
	fx.Out
	MysqlDanceDb *gorm.DB `name:"MysqlDanceDb"`
}

func (f *Result) Execute(service MysqlDbService) {
	//初始化
	dataSourceName := os.Getenv("MYSQL_DANCE_TIKTOK")
	if dbDance, err := service.MysqlGenerateGorm(dataSourceName, ""); nil != err {
		panic("初始化数据库失败")
	} else {
		f.MysqlDanceDb = dbDance
		service.UpdateModelDanceDb("1.0", dbDance)
	}
}

var Module = fx.Module("mysqlpkg", fx.Provide(
	func(
		l *zap.Logger,
		//s setting.Param,
		set setting.Param,
		//cService curlservice.Param,
	) (out Result, err error) {
		if svc, e := NewMysqlDb(
			l,
			set,
			//cService.CurlService,
		); e != nil {
			err = e
		} else {
			out.Execute(svc)
		}
		return
	},
),
)
